package day05;

public class MyBinarySearchDemo {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int number = 11;
        int index = binarySearchForIndex(arr, number);
        if (index == -1) {
            System.out.println("你要查找的数不存在");
        } else {
            System.out.println(index);
        }
    }


    private static int binarySearchForIndex(int[] arr, int number) {
        int min = 0;
        int max = arr.length - 1;
        while (min <= max) {
            int mid = (max + min) >> 1;
            if (arr[mid] > number) {
                max = mid - 1;
            } else if (arr[mid] < number) {
                min = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}
